package vn.vnu.uet.email;

import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.app.ProgressDialog;
import vn.vnu.uet.email.R;

public class SendMail extends DefaultActivity {
	String subject;
	String emailContent;
	String to;
	ProgressDialog dialog = null;
	EditText receiver, header, content;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState, R.layout.sendmail);

		this.setTitle("Gửi email");
		receiver = (EditText) findViewById(R.id.editTextTo);
		header = (EditText) findViewById(R.id.editTextSubject);
		content = (EditText) findViewById(R.id.editTextMessage);
		content.setVerticalScrollBarEnabled(true);
		content.setMovementMethod(new ScrollingMovementMethod());
		final Button send = (Button) this.findViewById(R.id.delete);
		send.setText("Gửi");

		send.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				to = receiver.getText().toString();
				subject = header.getText().toString();
				emailContent = content.getText().toString();
				new ProgressTask(SendMail.this).execute();

			}
		});

	}

	class ProgressTask extends AsyncTask<String, Void, Boolean> {
		private ProgressDialog dialog;

		/** progress dialog to show user that the backup is processing. */

		public ProgressTask(Context c) {
			super();
			dialog = new ProgressDialog(c);
		}

		/** application context. */

		protected void onPreExecute() {
			this.dialog.setMessage("Đang gửi..");
			this.dialog.show();
		}

		protected void onPostExecute(final Boolean success) {

			if (dialog.isShowing()) {
				dialog.dismiss();

			}
			ViewController.showInbox(SendMail.this);
		}

		protected Boolean doInBackground(final String... args) {
			to = receiver.getText().toString();
			subject = header.getText().toString();
			emailContent = content.getText().toString();
			SendMail.sendMail(Token.instance().user.userName + "@vnu.edu.vn",
					to, subject, emailContent);
			return true;
		}

	}

	public static void sendMail(String mailFrom, String mailTo,
			String mailSubject, String mailText) {

		@SuppressWarnings("serial")
		Properties props = new Properties() {
			{
				put("mail.smtp.auth", "true");
				put("mail.smtp.host", "ctmail.vnu.edu.vn");
				put("mail.smtp.port", "25");
				// put("mail.smtp.starttls.enable", "true");
				put("mail.debug", "true");
			}
		};

		//
		// Creates a mail session. We need to supply username and
		// password for Ctmail authentication.
		//
		Session session = Session.getInstance(props, new Authenticator() {
			@Override
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(
						Token.instance().user.userName,
						Token.instance().user.password);
			}
		});

		try {
			//
			// Creates email message
			//
			Message message = new MimeMessage(session);
			message.setSentDate(new Date());
			message.setFrom(new InternetAddress(mailFrom));
			message.setRecipient(Message.RecipientType.TO, new InternetAddress(
					mailTo));
			message.setSubject(mailSubject);
			message.setText(mailText);

			//
			// Send a message
			//
			Transport.send(message);
		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}
}